﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RadinfFakturer
{
    class Money
    {
        private static string[] _unity = new string[] { "zero",
                                                        "jeden", 
                                                        "dwa", 
                                                        "trzy", 
                                                        "cztery", 
                                                        "pięć", 
                                                        "sześć", 
                                                        "siedem", 
                                                        "osiem", 
                                                        "dziewięć"};
        private static string[] _overDozen = new string[] { "zero", 
                                                            "jedenaście", 
                                                            "dwanaście", 
                                                            "trzynaście", 
                                                            "czternaście", 
                                                            "piętnaście", 
                                                            "szesnaście", 
                                                            "siedemanście", 
                                                            "osiemnaście", 
                                                            "dziewiętnaście" };
        private static string[] _dozens = new string[] { "zero", 
                                                         "dziesięć", 
                                                         "dwadzieścia", 
                                                         "trzydzieści", 
                                                         "czterdzieści", 
                                                         "pięćdziesiąt", 
                                                         "sześćdziesiąt", 
                                                         "siedemdziesiąt", 
                                                         "osiemdziesiąt", 
                                                         "dziewięćdziesiąt" };
        private static string[] _houndreds = new string[] { "zero", 
                                                            "sto", 
                                                            "dwieście", 
                                                            "trzysta", 
                                                            "czterysta", 
                                                            "pięćset", 
                                                            "sześćset", 
                                                            "siedemset", 
                                                            "osiemset", 
                                                            "dziewięćset" };
        private static List<string[]> _numbers = new List<string[]>(new[] { _unity, _dozens, _houndreds });

        public Money()
        {
        }

        public static string getValue(double value)
        {
            string prefix = string.Empty;
            string result = string.Empty;
            string fraction = "00";
            string integers = value.ToString();
            string decimalSeparator = System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
            int numberIndexer = 0;
            if (integers.Contains(decimalSeparator))
            {
                fraction = integers.Substring(integers.IndexOf(decimalSeparator) + 1);
                if (fraction.Length == 1)
                    fraction += "0";
                integers = integers.Substring(0, integers.IndexOf(decimalSeparator));
            }

            for (int i = integers.Length - 1; i >= 0; i--)
            {
                int val = int.Parse(integers[i].ToString());
                int nextVal = 0;
                if (i > 0)
                    nextVal = int.Parse(integers[i - 1].ToString());
                if (val != 0)
                {
                    if (nextVal == 1 && numberIndexer == 0)
                    {
                        result = _overDozen[val] + " " + result;
                        i--;
                        numberIndexer++;
                    }
                    else
                    {
                        result = _numbers[numberIndexer][val] + " " + result;
                    }
                }
                numberIndexer++;
                if (numberIndexer == 3 && i > 0)
                {
                    if (nextVal == 1)
                        result = "tysiąc " + result;
                    else if (nextVal > 1 && nextVal < 5)
                        result = "tysiące " + result;
                    else
                        result = "tysięcy " + result;
                    numberIndexer = 0;
                }
            }
            result = result + "zł " + fraction + " gr";
            return result;
        }
    }
}
